package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.sms.model.SmsCode;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.resource.builder.sms.SmsBuilder;
import cn.signalpha.modules.resource.utils.SmsUtil;
import cn.signalpha.modules.shop.dto.SellerApplyDTO;
import cn.signalpha.modules.shop.entity.SellerApply;
import cn.signalpha.modules.shop.entity.SellerCategory;
import cn.signalpha.modules.shop.service.SellerApplyService;
import cn.signalpha.modules.shop.service.SellerCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/seller/apply")
@Api(value = "商户申请", tags = "商户申请")
public class ApiSellerApplyController extends BaseController {

	private final SmsBuilder smsBuilder;
	private final SellerApplyService sellerApplyService;
	private final SellerCategoryService sellerCategoryService;

	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "商户申请记录", notes = "")
	public R<List<SellerApply>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		List<SellerApply> list = sellerApplyService.list(Wrappers.<SellerApply>query().lambda()
			.eq(SellerApply::getUserId, getUser().getUserId()).orderByDesc(SellerApply::getId)
			.eq(ObjectUtil.isNotEmpty(params.get("status")), SellerApply::getStatus, params.get("status"))
		);
		return R.data(list);
	}

	@GetMapping("/info")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "商户申请详情", notes = "传入id")
	public R<SellerApply> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(sellerApplyService.getById(id));
	}

    @PostMapping("/save")
	@ApiOperationSupport(order = 10)
    @ApiOperation(value = "商户申请新增", notes = "传入sellerApply")
    public R save(@Valid @RequestBody SellerApplyDTO sellerApplyDTO) {
		//测试环境不需要验证码
//		ConfigurableEnvironment environment = new StandardEnvironment();
//		String activeProfiles = environment.getActiveProfiles()[0];
//		if(activeProfiles.equals(AppConstant.PROD_CODE)){
//		}

		Assert.isTrue(smsBuilder.template().validateMessage(new SmsCode().setPhone(sellerApplyDTO.getMobile()).setValue(sellerApplyDTO.getSmsCode())), SmsUtil.VALIDATE_FAIL);
		sellerApplyDTO.setType(1);
		sellerApplyDTO.setUserId(getUser().getUserId());
		sellerApplyDTO.setStatus(0);
		sellerApplyDTO.setCreateTime(new Date());
		SellerApply sellerApply = Objects.requireNonNull(BeanUtil.copy(sellerApplyDTO, SellerApply.class));
		sellerApplyService.sellerApplyCheckout(sellerApply);
        return R.status(sellerApplyService.save(sellerApply));
    }

	@PostMapping("/update")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "商户申请修改", notes = "传入sellerApply")
	public R update(@Valid @RequestBody SellerApplyDTO sellerApplyDTO) {
		//测试环境不需要验证码
//		ConfigurableEnvironment environment = new StandardEnvironment();
//		String activeProfiles = environment.getActiveProfiles()[0];
//		if(activeProfiles.equals(AppConstant.PROD_CODE)){
//		}

		Assert.isTrue(smsBuilder.template().validateMessage(new SmsCode().setPhone(sellerApplyDTO.getMobile()).setValue(sellerApplyDTO.getSmsCode())), SmsUtil.VALIDATE_FAIL);
		SellerApply sellerApply = sellerApplyService.getById(sellerApplyDTO.getId());
		Assert.isNull(sellerApply, MsgConstant.OBJECT_NULL);
		Assert.isTrue(sellerApply.getUserId().equals(getUser().getUserId()), MsgConstant.UNAUTHORIZED);
		Assert.isFalse(sellerApply.getStatus() == 1, "申请已审核通过，不允许修改");

		sellerApplyDTO.setType(1);
		sellerApplyDTO.setStatus(0);
		sellerApplyDTO.setRemark("");
		SellerApply sellerApplyUpdate = Objects.requireNonNull(BeanUtil.copy(sellerApplyDTO, SellerApply.class));
		sellerApplyService.sellerApplyCheckout(sellerApplyUpdate);
		return R.status(sellerApplyService.updateById(sellerApplyUpdate));
	}

	@GetMapping("/category/list")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "商户分类列表", notes = "")
	public R<List<SellerCategory>> list() {
		QueryWrapper<SellerCategory> queryWrapper = Wrappers.query();
		queryWrapper.lambda().orderByDesc(SellerCategory::getSort).orderByDesc(SellerCategory::getId);
		return R.data(sellerCategoryService.list(queryWrapper));
	}

}
